// var HOST = "http://103.251.90.136";
var HOST = "";
var REGISTER_REGEXS = {
    mobile: /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/,
    qq: /[1-9][0-9]{4,14}/,
    weixin: /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/,
    password: /\w{6,16}/
};

$(init);

function init() {

    promise('GET', '/buyer/weixin_binded', null, function (e) {


        if(e.data !== null) {
            e.data.userType = 0;
            saveCookie(e.data);
            window.location.href = "http://m.jsz800.com/html/main-buy.html";
        }
        if(e.data == null) {
            alert('请先完成绑定！');
        }


    }, null);
}

function saveCookie(data) {
    data.buyerBankList = [data.buyerBankList[data.buyerBankList.length - 1]];
    Cookies.set('cko', JSON.stringify(data), {
        expires: 999
    });
}

function promise(method, url, data, cb, err) {

    var promise = $.ajax({
        type: method,
        url: HOST + url,
        dataType: "json",
        contentType: "application/json",
        data: data,
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true
    });

    promise.then(function (e) {

        if (e.code === 0) {
            cb(e);
        } else if (e.code === 99) {
            alert('请先完成绑定！');
            if (err !== null) err(e);
        } else if (e.code === -1) {
            alert('系统出错了！');
        }

    });
}

function promiseU2(method, url1, url2, data1, data2, cb, err) {
    $.when(
        $.ajax({
            type: method,
            url: HOST + url1,
            dataType: "json",
            contentType: "application/json",
            data: data1,
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true
        }),
        $.ajax({
            type: method,
            url: HOST + url2,
            dataType: "json",
            contentType: "application/json",
            data: data2,
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true
        })
    ).done(function (e1, e2) {
        if ((e1[0].code === 0) && (e2[0].code === 0)) {
            cb(e1[0].data, e2[0].data);
        } else if ((e1[0].code === 99) || (e2[0].code === 99)) {
            alert('请先完成绑定！');
        } else if ((e1[0].code === -1) || (e2[0].code === -1)) {
            alert('请先完成绑定！');
        }

    });
}

function saveUser() {

    var mobile = $("#mobile").val();
    var password = $("#password").val();
    // var invitecode = $("#invitecode").val();
    var smscode = $("#smscode").val();

    var wx_obj = {
        invitecode: '',
        mobile: mobile,
        password: password
    };

    var lg_obj = {
        mobile: mobile,
        password: password
    };

    if(!REGISTER_REGEXS.mobile.test($('#mobile').val())) {
        alert('请输入正确的手机号码！');
        return;
    }else if(!REGISTER_REGEXS.password.test($('#password').val())) {
        alert('密码格式错误！');
        return;
    }

    // promiseU2('POST', '/buyer/weixin/register', '/buyer/buyer_login', JSON.stringify(wx_obj), JSON.stringify(lg_obj), cbU2, null);

    promise('POST', '/buyer/weixin/register', JSON.stringify(wx_obj), cbWeixin, null);
    // promise('POST', '/buyer/buyer_login', JSON.stringify(lg_obj), cbLogin, null);
}

function cbU2(e1, e2) {
    // console.log(e1);
    // console.log(e2);
    var ret2 = e2;
    ret2.userType = 0;
    ret2.password = $('#password').val();
    saveCookie(ret2);
    window.location.href = "http://m.jsz800.com/html/main-buy.html";
}

function cbWeixin(e) {
    window.location.href = "http://m.jsz800.com/html/main-buy.html";
    // alert('e.code=' + e.code);
}

// function cbLogin(e) {
//     e.userType = 0;
//     e.password = $('#password').val();
//     saveCookie(e);
//     window.location.href = "http://test.jsz800.com/html/main-buy.html";
// }

function getSmsCode() {
    if(!REGISTER_REGEXS.mobile.test($('#mobile').val())) {
        alert('请输入正确的手机号码！');
        return;
    }
    var obj = {
        'mobilephone': $('#mobile').val()
    };
    promise('GET', '/sms_send', obj, cbShowBtn, null);
}

function cbShowBtn(e) {
    console.log(e);
    if(e.code === 0) {
        $('#smscode-btn').attr("disabled", true);
        // $('#signup-btn').attr('disabled', false);
        doCounter(60, 'smscode-btn');
    }
}

function goToSignUp() {
    window.location.href = 'http://m.jsz800.com/html/wx-signup.html';
}

function doCounter(count, id) {
    setTimeout(function() {
        $(`#${id}`).text(--count);
        if (count) {
            doCounter(count, id);
        } else {
            $(`#${id}`).text('获取验证码');
            $(`#${id}`).attr("disabled", false);
        }
    }, 1000);
}